package com.xhapi.messageQueue.producer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhapi.pojo.entity.HistoryQuestionByAi;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String exchange, String routingKey, List<HistoryQuestionByAi> message){
        ObjectMapper objectMapper=new ObjectMapper();
        try{
            String jsonToString=objectMapper.writeValueAsString(message);
            rabbitTemplate.convertAndSend(exchange,routingKey,jsonToString);
        }catch (Exception e){
            throw new RuntimeException("消息发送失败");
        }

    }

}
